package com.fmyexample.v2mcsecuritybootjdbc.modular.controller;


import com.fmy.mcsecurity.token.TokenManager;
import com.fmy.mcsecurity.token.model.SecurityToken;
import com.fmyexample.v2mcsecuritybootjdbc.sucerity.CuUserDetails;
import lombok.AllArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ms
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/login-token")
@AllArgsConstructor
public class LoginTokenController {


    private final TokenManager tokenManager;


    /**
     * 获取当前用户，登录token信息
     */
    @GetMapping("/auth/user-login-token")
    public List<SecurityToken> userLoginToken() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = null;
        if (principal instanceof CuUserDetails) {
            userId = ((CuUserDetails) principal).getUserId();
        }
        return tokenManager.getUserToToken("administration", userId);
    }

    /**
     * 剔出用户
     */
    @PostMapping("/kick")
    public String kick(String token, @RequestParam(defaultValue = "抱歉，您被管理员剔出，请重新登录") String cause) {
        tokenManager.kick(token, cause);
        return "剔出成功";
    }

    /**
     * 剔出某个用户某个客户端所有登录
     */
    @PostMapping("/kick-user")
    public String kickUser(String userId, @RequestParam(defaultValue = "抱歉，您被管理员剔出，请重新登录") String cause) {
        tokenManager.kickUserKey("1", userId, cause);
        return "剔出成功";
    }


    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

